
// 这部分是那个注册登录的input的placeholder变小回缩
const searchFocus = document.getElementById('search-focus');
const keys = [
  { keyCode: 'AltLeft', isTriggered: false },
  { keyCode: 'ControlLeft', isTriggered: false },
];

window.addEventListener('keydown', (e) => {
  keys.forEach((obj) => {
    if (obj.keyCode === e.code) {
      obj.isTriggered = true;
    }
  });

  const shortcutTriggered = keys.filter((obj) => obj.isTriggered).length === keys.length;

  if (shortcutTriggered) {
    searchFocus.focus();
  }
});

window.addEventListener('keyup', (e) => {
  keys.forEach((obj) => {
    if (obj.keyCode === e.code) {
      obj.isTriggered = false;
    }
  });
});

// 检查登录
let userid= document.querySelector('#uid');
let LoginPS= document.querySelector('#Loginpassword');
document.querySelector('.loginM form').addEventListener('submit',e=>{
  if(!checkLogin()){
      e.preventDefault();
  }else{
    window.location.href="http://localhost:8080/index";
  }})
  function checkLogin(){
    const uidvalue = userid.value.trim();
    const LoginPSvalue = LoginPS.value.trim();
    if(uidvalue === ""){
      printError(userid,"id不能为空");
      return false;
    }else if (!validTextNum(uidvalue)){
      printError(userid,"id不正确");
      return false;
    }else{
      removeError(userid);
    }
    if(LoginPSvalue === ""){
      printError(LoginPS,"密码不能为空");
     
      return false;
    }else{
       removeError(LoginPS);
    }
    return true
  }

  function validTextNum(text) {
    const reg = /^[0-9]+$/;
    return reg.test(text);
    }
// 检查注册
let Rname = document.querySelector('#Rname');
let Remail = document.querySelector('#Remail');
let Rps = document.querySelector('#Rps');
let Rps2 = document.querySelector('#Rps2');
document.querySelector('.signUpM form').addEventListener('submit',e=>{
    if(!checkSignUp()){
        e.preventDefault();
    }else{
      alert("注册成功");
      window.location.href="http://localhost:8080/index";
    }})

function checkSignUp(){

  const Namevalue = Rname.value.trim();
  const PSvalue = Rps.value.trim();
  const PSvalue2 = Rps2.value.trim();
  const Remailvalue = Remail.value.trim();

  if(Namevalue === ""){
    printError(Rname,"姓名不能为空");

    return false;
  }else{
    
    removeError(Rname);
  }

  if(  PSvalue2 === ""){
    printError(Rps2,"密码不能为空");
    return false;
  }else{
    
    removeError(Rps2)
  }
  if(PSvalue === ""){
      printError(Rps,"密码不能为空");
     
      return false;
    }else{
       removeError(Rps);
    }
    if(PSvalue != PSvalue2){
      printError(Rps,"两次密码不相等");
      printError(Rps2,"两次密码不相等");
      return false;
  }else{
    removeError(Rps);
    removeError(Rps2);
  }
    if(Remailvalue===""){
      printError(Remail,"邮箱不能为空");
      return false;
  }else if(!valiEmail(Remailvalue)){//valiEmail返回的false表示邮箱不正确，则对其取反为true，if执行
     printError(Remail,"邮箱格式不正确，请重新输入");
     return false;
  }else{
    removeError(Remail);
  }
  return true;
}

function printError(input,message){
    formControl = input.parentElement;//所验证实体（输入框）的父组件
    const ivFeedback = formControl.querySelector('.invalid-feedback');
    input.classList.remove('is-valid');
    input.classList.add('is-invalid');
    ivFeedback.innerHTML = message;
}  
function removeError(input){
    input.classList.remove('is-invalid');
    input.classList.add('is-valid');
} 
function valiEmail(email){
  const regEmail = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
  //将传过来的email值与定义的邮箱范围相比较
  return regEmail.test(email);//返回true或false
}

// 编辑
let edit = document.querySelector(".edit")
let unedit = document.querySelector(".unedit")
let controlPost = document.querySelector(".controlPost")
let homepost = document.querySelectorAll(".showPost")
edit.addEventListener("click",function(){
  homepost.forEach(function(element){
    element.classList.add("hidden")
});
controlPost.classList.remove("hidden")
})
unedit.addEventListener("click",function(){
  homepost.forEach(function(element){
    element.classList.remove("hidden")
});
controlPost.classList.add("hidden")
})

//新增帖子那部分
let Title = document.querySelector('#Title');
let Content = document.querySelector('#Content');

document.querySelector('.addPost form').addEventListener('submit',e=>{
    if(!checkAddPost()){
        e.preventDefault();
    }
  })
  function checkAddPost(){
    const Titlevalue = Title.value.trim();
    const Contentvalue = Content.value.trim();
    if(Titlevalue === ""){
      printError(Title,"标题不能为空");
      return false;
    }else{
      removeError(Title);
    }
    if(Contentvalue === ""){
      printError(Content,"内容不能为空");
      return false;
    }else{
      removeError(Content);
    }
    return true;
  }

